import Vue from "vue";

function getStoraged(key) {
  let data = uni.getStorageSync(key);
  if (data) {
    return data;
  } else {
    return '';
  }
};

Vue.prototype.$drawPoster = (data, that) => {

  return new Promise((resolve, reject) => {
    let poster = "https://chenxiao.obs.cn-north-1.myhuaweicloud.com/img/46_1683552194598325210.pSpvUIW4su8Jf8d297dd7330742bc8dff945a41d23fc.png"
    let systemInfo = getStoraged("systemInfo")
    console.log("systemInfosystemInfosystemInfosystemInfo", systemInfo);
    let canvasWidth = getStoraged('systemInfo').screenWidth
    let canvasHeight = getStoraged('systemInfo').screenHeight / 2.22
    let bl = getStoraged('systemInfo').screenHeight / getStoraged('systemInfo').screenWidth
    // let bl = 1.25
    uni.getImageInfo({
      src: poster,
      success: (res) => {
        // uni.showLoading({
        //   title: '加载中...',
        //   mask: true
        // })
        console.log("res", res);
        let context = uni.createCanvasContext(data.canvasId || 'my-canvas', that)
        console.log("context", context);
        // 整个canvas填充背景
        // context.setFillStyle('#f8f8f8');
        // context.fillRect(0, 0, 500, 900);
        // 绘制背景图
        console.log("path", res.path);
        context.drawImage(res.path, 0, 0, canvasWidth, canvasHeight)
        context.fill();
        // 绘制logo
        context.drawImage("/static/images/common/defaultUserHead.png", 10 * bl, 10 * bl, 15 * bl, 15 * bl)
        context.fill();
        // 绘制标题
        context.setFontSize(16); // setFontSize() 设置字体字号
        context.setFillStyle('#fff'); // setFillStyle() 设置字体颜色
        // context.font = 'normal normal 16px sans-serif';
        context.fillText("餐饮信息网", 30 * bl, 20 * bl); // （文字，x，y）
        context.fill();

        // 绘制介绍
        context.setFontSize(14); // setFontSize() 设置字体字号
        context.setFillStyle('#929294'); // setFillStyle() 设置字体颜色
        context.fillText("附近的老板和餐饮从业者都在这", 95 * bl, 20 * bl); // （文字，x，y）
        context.fill();
        // 绘制求职或者招聘类型
        context.setFontSize(50); // setFontSize() 设置字体字号
        context.setFillStyle('#fff'); // setFillStyle() 设置字体颜色
        context.fillText(data.type || "", 20 * bl, 60 * bl); // （文字，x，y）
        context.fill();
        // 绘制标签长方形
        let tags = data.tag;
        // tags = ["哈哈哈", "哈哈哈哈哈哈", "哈哈哈哈哈哈"]
        let x = 80 * bl; // 标签起始 x 坐标
        let y = 35 * bl; // 标签起始 y 坐标
        let lineHeight = 18; // 标签行高
        let tagGap = 10; // 标签之间的间隔
        context.setFontSize(14); // 设置字体大小
        context.setTextBaseline('top'); // 设置文本垂直对齐方式为文本顶部对齐
        console.log(tags);
        tags.forEach(tag => {
          if (!tag) return false
          let tagWidth = context.measureText(tag).width + (5 * bl); // 标签宽度，加上左右边距
          if (x + tagWidth > canvasWidth) { // 如果标签超出画布宽度，换行
            x = 80 * bl;
            y += lineHeight + tagGap;
          }

          // context.beginPath(); // 开始绘制路径
          // context.rect(x, y, tagWidth, lineHeight); // 绘制标签矩形
          // context.setFillStyle('transparent'); // 设置矩形填充色
          // context.fill(); // 填充矩形
          // context.setStrokeStyle('#FDEEC4'); // 设置矩形边框颜色
          // context.stroke(); // 绘制矩形边框
          // context.closePath()
          context.setFillStyle('#FDEEC4'); // 设置文本颜色
          context.fillText(tag, x + 10, y + 10); // 绘制标签文本
          x += tagWidth + tagGap; // 更新 x 坐标
          console.log("xxxxxxxxxxxxx", x);
        });
        context.fill();
        //绘制介绍
        let text2 = data.desc || "暂无介绍";
        let maxWidth2 = canvasWidth / 1.5; // 最大宽度
        let maxLines2 = 3; // 最多显示的行数
        let fontSize2 = 13; // 字体大小
        let lineHeight2 = fontSize2 * 1.5; // 行高
        let ellipsis2 = '...'; // 省略号
        let x2 = 25 * bl; // 起始 x 坐标
        let y2 = 75 * bl; // 起始 y 坐标

        let textWidth2 = context.measureText(text2).width;
        let lines = [];
        let lastLineIndex = 0;
        for (let i = 1; i <= text2.length; i++) {
          let lastLine = i > 1 ? lines[lines.length - 1] : '';
          let line = text2.substring(lastLineIndex, i);
          let lineWidth = context.measureText(line).width;
          let lastLineWidth = context.measureText(lastLine).width;
          if (lineWidth <= maxWidth2 && i < text2.length) {
            continue;
          }

          if (lines.length < maxLines2 && i < text2.length) {
            lines.push(line.substring(0, line.length - 1));
            lastLineIndex = i - 1;
            i--;
          } else {
            if (lastLine) {
              let lastLineLength = lastLine.length - ellipsis2.length;

              if (lastLineLength > 0) {
                lines[lines.length - 1] = lastLine.substring(0, lastLineLength) + ellipsis2;
              }
            } else {
              lines[0] = text2
            }

            break;
          }
        }

        console.log(lines);
        lines.forEach((line, index) => {
          context.fillText(line, x2, y2 + index * lineHeight2);
        });
        context.fill();
        // 绘制定位图标
        context.drawImage("/static/images/common/address.png", 20 * bl, 110 * bl, 10 * bl, 11 * bl)
        context.fill();
        //绘制位置信息
        context.setFillStyle('#FDEEC4'); // 背景颜色 	#00ff00 = green 	#ff0000 = red 	#ffffff
        context.setFontSize(13);
        context.fillText(data.address || "暂无位置信息", 31 * bl, 112.5 * bl);
        context.fill();
        // 绘制立即联系按钮
        context.drawImage("/static/images/common/ljlx.png", 30 * bl, 130 * bl, 80 * bl, 25 * bl)
        context.fill();
        console.log("130", context.canvasId);
        context.draw(false, () => {
          uni.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: canvasWidth,
            height: canvasHeight,
            canvasId: context.canvasId,
            success: (res) => {
              console.log("40", res);
              resolve(res.tempFilePath)
            }
          }, that)

        });


        // setTimeout(() => {
        //   uni.canvasToTempFilePath({
        //     canvasId: context.canvasId,
        //     success: (res) => {
        //       console.log("40", res);
        //       resolve(res.tempFilePath)
        //     }
        //   })
        // }, 500)

      }
    })
  })


}

let share = {
  // mixins: ['modal'],
  data() {
    return {
      share: {
        title: "全国餐饮人的信息服务平台，专注餐饮的平台更懂你",
        // 分享页面标题
        path: "/pages/index",
        // 分享页面的路径
        // imageUrl: "https://chenxiao.obs.cn-north-1.myhuaweicloud.com/img/13_1680788597968084151.EnmisQx4u0M58c226bfd804746b801e3eb4c23f4ebc8.jpg",
        imageUrl: "https://chenxiao.obs.cn-north-1.myhuaweicloud.com/img/13_1680789438427854430.eterYuCrlWYubbb1b3348e59dda8aa3997977bb17a78.jpg",
        desc: "全国餐饮人的信息服务平台，专注餐饮的平台更懂你",
        content: "全国餐饮人的信息服务平台，专注餐饮的平台更懂你",
      },
    };
  },
  onShareAppMessage(res) {
    return {
      title: this.share.title,
      path: this.share.path + `?shareUserId=${getStoraged("user_id")}`,
      imageUrl: this.share.imageUrl,
      desc: this.share.desc,
      content: this.share.content,
      success(res) {
        uni.showToast({
          title: "分享成功"
        });
        console.log(this.$refs.modal, "999999999");
        uni.$emit('share-success')
      },
      fail(res) {
        uni.showToast({
          title: "分享失败",
          icon: "none"
        });
        uni.$emit('share-fail')
      },
      complete() {
        console.log(this.$refs.modal, "999999999");
      },
    };
  },
  onShareTimeline(res) {
    return {
      title: this.share.title,
      query: this.share.path + `?shareUserId=${getStoraged("user_id")}`,
      imageUrl: this.share.imageUrl,
      desc: this.share.desc,
      content: this.share.content,
      success(res) {
        // uni.showToast({
        //   title: "分享成功"
        // });
        console.log(this.$refs.modal, "999999999");
        this.$refs.modal.showModal();
      },
      fail(res) {
        uni.showToast({
          title: "分享失败",
          icon: "none"
        });
        console.log(this.$refs.modal, "999999999");
        this.$refs.modal.showModal();
      },
    };
  },
};

Vue.mixin(share);
export default share